import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;


public class BoxList extends ArrayList<Box> {
	private TileList reachableSurroundingTiles;
	private Coord playerPos;
	private TileList boxConversion;
	
	public BoxList() {
		reachableSurroundingTiles = new TileList();
	}
	
	public TileList convertToTiles(){
		boxConversion=new TileList();
		
		for(int i=0;i<this.size();i++){
			boxConversion.add(this.get(i).position);
		}
		
		return boxConversion;
	}
	
	public void addReachableSurroundingTile(Coord tile) {
		reachableSurroundingTiles.add(tile);
	}
	
	public TileList getReachableSurroundingTiles() {
		return reachableSurroundingTiles;
	}
	
	public void setPlayerPos(Coord playerPos) {
		this.playerPos = playerPos;
	}
	
	public boolean isItSameBoxList(BoxList boxList) {
		//Ugly comparison in n^2-time. Please change!
		
		
		for (int i=0 ; i<boxList.size() ; i++) {
			Coord box1Coord = boxList.get(i).getCoord();
			boolean isInList = false;
			
			for (Box box2 : this) {
				if (box1Coord.isItSameCoord(box2.getCoord())) {
					//Found the coord
					isInList = true;
					break;
				}
			}
			if(!isInList) return false;
		}
		if (boxList.playerPos.isItSameCoord(this.playerPos)) {
			return true;
		} else {
			return DirectionMap.getDTile(playerPos).isVisited();
		}
			
		//return isItSameTileList(boxList.getReachableSurroundingTiles());
	

		
		/*
		
		System.out.println("ddd");
		for (int i = 0 ; i < boxList.size() ; i++) {
			Box box1 = boxList.get(i);
			boolean isInList = false;
			for (Box box2 : this) {
				if (box1.getCoord().isItSameCoord(box2.getCoord())) {
					isInList = true;
					break;
				}
			}
			
			if (!isInList) return false;
		}
		
		//If we get here, the boxlists are identical. 
		//Now compare the reachable surrounding tiles
		return isItSameTileList(boxList.getReachableSurroundingTiles());
		*/
	}
	
	public boolean isItSameTileList(TileList tileList) {
		//Ugly comparison in n^2-time. Please change!
		
		//TODO check that we don't send in the same list of tiles
		for (int i=0 ; i<tileList.size() ; i++) {
			Coord tile = tileList.get(i);
			boolean isInList = false;
			
			for (Coord tile2 : reachableSurroundingTiles) {
				if (tile.isItSameCoord(tile2)) {
					//Found the coord
					isInList = true;
					break;
				}
			}
			if(!isInList) return false;
		}
		return true;
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		/*
		
		System.out.println("Is it same tileLisT?");
		boolean isInList = false;
		for (int i = 0 ; i < tileList.size() ; i++) {
			Coord tile1 = tileList.get(i);
			isInList = false;
			for (Coord tile2 : reachableSurroundingTiles) {
				if (tile1.isItSameCoord(tile2)) {
					isInList = true;
					break;
				}
			}
		
			if (!isInList) return false;
		}
		
		System.out.println("Found ");
		for(Box box : this) {
			Coord c = box.getCoord();
			System.out.print("(" + c.x + "," + c.y + "), ");
		}
		System.out.println("");
		System.out.println("");
		
		return true;
		*/
	}
	public boolean isBoxOnTile(Coord tile) {
		for (Box box : this) {
			if (tile.isItSameCoord(box.getCoord())) return true; 
		}
		return false;
	}
}
